'''
https://leetcode.cn/problems/count-complete-subarrays-in-an-array
'''

from collections import Counter
from typing import List


class Solution:
    def countCompleteSubarrays(self, nums: List[int]) -> int:
        n = len(nums)
        counter = Counter(nums)
        all_size = len(counter)
        l = r = 0
        cnt = Counter()
        size = 0
        res = 0
        while l < n:
            while r < n and size < all_size:
                size += cnt[nums[r]] == 0
                cnt[nums[r]] += 1
                r += 1
            if size == all_size:
                res += n - r + 1
            cnt[nums[l]] -= 1
            size -= cnt[nums[l]] == 0
            l += 1
        return res


nums = [1,3,1,2,2]
print(Solution().countCompleteSubarrays(nums))